﻿using System;
using System.Data.Entity;
using Kay.LazarToDo.Win.Common;
using Kay.LazarToDo.Win.Models;
using System.Linq;

namespace Kay.LazarToDo.Win.AppServices
{
    /// <summary>
    /// 项目应用服务
    /// </summary>
    public class ProjectAppService
    {
        /// <summary>
        /// 获取所有项目
        /// </summary>
        /// <returns></returns>
        public IQueryable<TjlProject> GetAllList()
        {
            var query = GlobalVariables.DbContext.TjlProjects.AsQueryable();
            query = query.OrderByDescending(r => r.Shown).ThenByDescending(r => r.CreationTime);
            return query;
        }

        /// <summary>
        /// 通过显示状态获取项目
        /// </summary>
        /// <param name="show"></param>
        /// <returns></returns>
        public IQueryable<TjlProject> GetByShown(bool show)
        {
            var query = GlobalVariables.DbContext.TjlProjects.AsQueryable();
            query = query.Where(r => r.Shown == show);
            query = query.OrderByDescending(r => r.CreationTime);
            return query;
        }

        /// <summary>
        /// 检查项目名称是否存在
        /// </summary>
        /// <param name="name">项目名称</param>
        /// <returns>True存在，否则为False</returns>
        public bool CheckName(string name)
        {
            var query = GlobalVariables.DbContext.TjlProjects.AsQueryable();
            return query.Any(r => r.Name == name);
        }

        /// <summary>
        /// 保存项目
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public TjlProject SaveProject(string name)
        {
            var project = GlobalVariables.DbContext.TjlProjects.Create();
            project.Id = Guid.NewGuid();
            project.Name = name;
            project.Shown = true;
            project.CreationTime = DateTime.Now;
            var projectEntity = GlobalVariables.DbContext.TjlProjects.Add(project);
            GlobalVariables.DbContext.SaveChanges();
            return projectEntity;
        }

        /// <summary>
        /// 更新或删除项目
        /// </summary>
        /// <param name="id">项目标识</param>
        /// <param name="del">删除</param>
        /// <param name="shown">显示</param>
        /// <returns></returns>
        public TjlProject UpdateOrDeleteProject(Guid id, bool del, bool shown)
        {
            var entity = GlobalVariables.DbContext.TjlProjects.Find(id);
            if (entity == null)
                return null;
            if (del)
            {
                GlobalVariables.DbContext.Entry(entity).Collection(p => p.LazarToDoCollection).Load();
                if (entity.LazarToDoCollection.Any())
                {
                    return entity;
                }
                GlobalVariables.DbContext.TjlProjects.Remove(entity);
            }
            else
            {
                entity.Shown = shown;
            }
            GlobalVariables.DbContext.SaveChanges();
            return entity;
        }
    }
}
